/**
 * Created by Ru on 3/3/17.
 */


var mongoose = require('mongoose');


var sensorDataSchema = new mongoose.Schema({

    faultId:{
        type:String,
        required:true
    },
    tempeDataList:
        [{date:Date,dataList:[Number]}]
    ,

    currentDataList:
        [{date:Date,dataList:[Number]}]
    ,

});
sensorDataSchema.methods.addTempeData = function (tempList,callback) {
    var obj = {
        date: Date.now(),
        dataList:tempList
    }


    this.tempeDataList.push(obj);
    this.save();
};


sensorDataSchema.methods.addCurrentData = function (currentList,callback) {
    var obj = {
        date: Date.now(),
        dataList:currentList
    }


    this.currentDataList.push(obj);
    this.save();
};



var SensorData = module.exports = mongoose.model('sensorData',sensorDataSchema);




module.exports.addDataModel = function (faultId,callback) {

    var obj ={
        faultId:faultId
    }
    SensorData.create(obj,callback);
}

module.exports.historyDataByFaultId = function (faultId,callback) {

    var obj ={
        faultId:faultId
    }
    SensorData.find(obj,function (err,sensorDatas) {
        if(sensorDatas.length == 0){
            callback('notfind');
        }

        var sensorData = sensorDatas[0];

        callback(sensorData);

    });
}

module.exports.addTemperature = function (faultId,tempeList) {
    // [{date:Date,dataList:[{time:Date,value:Number}]}]


    if(tempeList && tempeList.length < 2){
        return;
    }

    console.log("faultid===" +faultId + "----"+tempeList)

    SensorData.find({faultId:faultId},function (err,sensorData) {
        if (err){
            throw err;
        }


        var sensordataresult = sensorData[0];
        console.log("sensordata---"+sensorData);

        if (sensorData.length != 0){//zhaodao

            console.log("找到了")
            sensordataresult.addTempeData(tempeList);
        }else {
            SensorData.addDataModel(faultId);
            setTimeout(function () {
                SensorData.addTemperature(faultId,tempeList)
            },100);
        }

    })
}

function sleep(numberMillis) {
    var now = new Date();
    var exitTime = now.getTime() + numberMillis;
    while (true) {
        now = new Date();
        if (now.getTime() > exitTime)
            return;
    }
}

module.exports.addCurrent = function (faultId,currentList) {
    // [{date:Date,dataList:[{time:Date,value:Number}]}]

    if(currentList && currentList.length < 2){
        return;
    }

    console.log("faultid===" +faultId + "----"+currentList)

    SensorData.find({faultId:faultId},function (err,sensorData) {
        if (err){
            throw err;
        }


        sleep(1);
        var sensordataresult = sensorData[0];
        console.log("sensordata---"+sensorData);

        if (sensorData.length != 0){//zhaodao

            console.log("找到了")
            console.log(currentList);
            sensordataresult.addCurrentData(currentList);
        }else {
            SensorData.addDataModel(faultId);
            setTimeout(function () {
                SensorData.addCurrent(faultId,currentList)
            },100);
        }

    })
}